/**
 * 弹出框插件
 * 依赖layer
 */

layui.define(['layer'], function (exports) {
    
    const show = (res, config, callback) => {
        let msg = '';
        if (typeof res === 'object') {
            msg = res.msg || '';
        } else {
            msg = res;
        }
        if (typeof msg !== 'string' || msg.length === 0) {
            return false;
        }
        
        if (typeof config === 'number') {
            let title = lang.dialog.success || '成功';
            if (config === 2) {
                title = lang.dialog.fail || '失败';
            }
            config = {
                icon: config,
                title: title,
                offset: '100px',
            };
        }
        
        let index = layer.alert(msg, config, function () {
            layer.close(index);
            if (typeof callback === 'function') {
                callback(res);
            }
        });
    };
    
    const toast = {
        /**
         * 显示错误消息
         * @param res
         * @param callback
         */
        error: (res, callback) => {
            show(res, 2, callback);
        },
        /**
         * 显示成功消息
         * @param res
         * @param callback
         */
        success: (res, callback) => {
            show(res, 1, callback);
        },
        /**
         * 自动拾取服务器响应消息
         * @param res
         * @param callback
         */
        response: (res, callback) => {
            if (res.code) {
                toast.error(res.msg ? res.msg : lang.operate.fail || '操作失败');
            } else {
                let message = res.msg ? res.msg : lang.operate.success || '操作成功';
                let type = typeof callback;
                if (type === 'function') {
                    toast.success(message, callback);
                } else if (type === 'undefined') {
                    callback = function () {
                        layer.closeAll();
                    };
                    toast.success(message, callback);
                } else if (type === 'string') {
                    toast.success(message);
                }
            }
        },
        /**
         * 顶部消息
         * @param message
         * @param success
         * @returns {boolean}
         */
        top: (message, success) => {
            
            let msg = '';
            if (typeof message === 'object') {
                success = (message.code || 0) === 0 ? 1 : 0;
                msg = message.msg || '';
            } else {
                msg = message;
            }
            
            if (typeof msg !== 'string' || msg.length === 0) {
                return false;
            }
            layer.msg(msg, {
                icon: success ? 1 : 2,
                anim: 'slideDown',
                offset: '100px'
            });
            return false;
        },
        /**
         * 中间消息
         * @param msg
         * @param success
         * @returns {boolean}
         */
        center: (msg, success) => {
            if (typeof msg !== 'string' || msg.length === 0) {
                return false;
            }
            layer.msg(msg, {
                icon: success ? 1 : 2,
                anim: 'slideDown'
            });
            return false;
        },
        /**
         * 晃动
         * @param msg
         * @param time
         * @returns {boolean}
         */
        shake: (msg, time) => {
            if (typeof msg !== 'string' || msg.length === 0) {
                return false;
            }
            time = parseInt(time);
            if (isNaN(time) || time < 1000) {
                time = 2000;
            } else if (time > 8000) {
                time = 5000;
            }
            layer.open({
                title: false,
                content: msg,
                icon: 2,
                time: time,
                btn: false,
                closeBtn: false,
                anim: 6,
                offset: '100px'
            });
        },
        
        /**
         *确认框
         * @param content
         * @param config
         */
        confirm: (content, config) => {
            
            content = content ? content : (lang.dialog.miss_content || 'Warning');
            
            return new Promise((resolve, reject) => {
                let _config = {
                    icon: 3,
                    offset: '180px',
                    btn: [lang.btn.ok || '确定', lang.btn.close || '关闭'],
                    title: lang.dialog.miss_tips || 'Warning',
                };
                if (typeof config === 'object') {
                    _config = config;
                }
                layer.confirm(content || '', _config, function () {
                    resolve();
                });
            });
        }
    };
    exports('toast', toast);
});